package com.fenghuajueli.idiomppp.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.entity.GuessAnswerEntity;
import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;

import java.util.List;

/**
 * 看图猜成语已经填充填充文字数据适配器
 */
public class IdiomGuessWordGridAdapter extends BaseAdapter {

    private List<GuessAnswerEntity> guessAnswerEntityList;
    private Context mContext;
    private LayoutInflater mInflater;
    private int itemWidth;
    private OnListClickListener<GuessAnswerEntity> onListClickListener;
    private int isBgStatus = 0;


    public IdiomGuessWordGridAdapter(Context context, List<GuessAnswerEntity> guessAnswerEntityList) {
        this.guessAnswerEntityList = guessAnswerEntityList;
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        itemWidth = ConvertUtils.dp2px(32);
    }

    public void refreshBg(int isBgStatus) {
        this.isBgStatus = isBgStatus;
        notifyDataSetChanged();
    }

    public void refreshData(List<GuessAnswerEntity> guessAnswerEntityList) {
        this.guessAnswerEntityList = guessAnswerEntityList;
        notifyDataSetChanged();
    }

    public void setOnListClickListener(OnListClickListener<GuessAnswerEntity> onListClickListener) {
        this.onListClickListener = onListClickListener;
    }

    @Override
    public int getCount() {
        return guessAnswerEntityList.size();
    }

    @Override
    public Object getItem(int i) {
        return guessAnswerEntityList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.idiom_fill_text_item_layout, null);
            holder.idiomText = convertView.findViewById(R.id.idiomText);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(itemWidth, itemWidth);
            convertView.setLayoutParams(layoutParams);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GuessAnswerEntity idiomPinEntity = guessAnswerEntityList.get(position);
        if (idiomPinEntity.getFill()) {
//            holder.idiomText.setBackgroundResource(R.mipmap.aa_grade_bg4);
            holder.idiomText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (idiomPinEntity.getFill()) {
                        onListClickListener.itemClick(position, idiomPinEntity);
                        idiomPinEntity.setFill(false);
                        idiomPinEntity.setFillText("");
                        notifyDataSetChanged();
                    }
                }
            });
        } else {
//            holder.idiomText.setBackgroundResource(R.mipmap.aa_tianci_bg);
            holder.idiomText.setOnClickListener(null);
        }
        holder.idiomText.setText(idiomPinEntity.getFillText());
        if (isBgStatus == 1) {
            holder.idiomText.setBackgroundResource(R.drawable.shape_guess_word_correct);
        } else if (isBgStatus == 2) {
            holder.idiomText.setBackgroundResource(R.drawable.shape_guess_word_error);
        } else {
            holder.idiomText.setBackgroundResource(R.drawable.shape_guess_word_normal);
        }

        Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/baotuxiaobaiti.ttf");
        holder.idiomText.setTypeface(tf);
        return convertView;
    }

    final class ViewHolder {
        TextView idiomText;
    }

    /**
     * 点击填充过得字时  取消填充到列表，恢复到底部可选的字列表
     *
     * @param text
     */
    public void recoveryItem(String text) {
        for (int i = 0; i < guessAnswerEntityList.size(); i++) {
            GuessAnswerEntity idiomPinEntity = guessAnswerEntityList.get(i);
            if (idiomPinEntity.getWord().equals(text)) {
                idiomPinEntity.setSelect(false);
            }
        }
        notifyDataSetChanged();
    }

}
